﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using QLTV_BLL;
using QLTV_Public;

namespace GUI
{
    public partial class uc_ViPham : DevExpress.XtraEditors.XtraUserControl
    {
        #region khai bao bien
        ViPham_BLL vp_bll = new ViPham_BLL();
        TextMessageBox box = new TextMessageBox();
        DataTable dt;
        DataTable dt1;
        #endregion

        #region LoadForm

        public uc_ViPham()
        {
            InitializeComponent();
        }

        private void uc_ViPham_Load(object sender, EventArgs e)
        {
            LoadData();
        }
        #endregion

        #region cac chuc nang
        private void btnThem_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            frmViPham frm = new frmViPham();
            if (frm.ShowDialog() == DialogResult.OK)
                LoadData();
        }

        private void btnSua_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (grv_Data.RowCount > 0)
            {
                try
                {
                    frmViPham frm = new frmViPham();
                    frm.maViPham = grv_Data.GetRowCellValue(grv_Data.FocusedRowHandle, colMaViPham).ToString();
                    if (frm.ShowDialog() == DialogResult.OK)
                        LoadData();
                }
                catch (Exception)
                {
                    XtraMessageBox.Show(box.focuesFillter,box.Caption,MessageBoxButtons.OK,MessageBoxIcon.Information);
                }
            }
            else
                XtraMessageBox.Show(box.NotData,box.Caption,MessageBoxButtons.OK,MessageBoxIcon.Warning);
        }

        private void btnXoa_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (grv_Data.RowCount > 0)
            {
                ViPham_Pubic vp = new ViPham_Pubic();
                try
                {
                    string ma = grv_Data.GetRowCellValue(grv_Data.FocusedRowHandle, colMaViPham).ToString();
                    vp.MaViPham = ma;
                    dt = vp_bll.TaoBang(box.sqlCheckExist("PhieuPhat", "MaViPham", ma));
                    dt1 = vp_bll.TaoBang(box.sqlCheckExist("PhieuBoiThuong", "MaViPham", ma));
                    if (dt.Rows.Count > 0 || dt1.Rows.Count > 0)
                    {
                        XtraMessageBox.Show("Mã vi pham: '" + ma + "' đang được sử dung.Không thể xóa.", box.Caption, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }
                    else if (XtraMessageBox.Show("Bạn có chắc muốn xóa Mã: '" + ma + "' này không?", box.Caption_Confirm, MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
                    {
                        try
                        {
                            vp_bll.Delete(vp);
                            LoadData();
                        }
                        catch (Exception)
                        {
                            XtraMessageBox.Show(box.DeleteFail, box.Caption_Fail, MessageBoxButtons.OK, MessageBoxIcon.Error);
                        }
                    }
                }
                catch (Exception)
                {
                    XtraMessageBox.Show(box.focuesFillter,box.Caption,MessageBoxButtons.OK,MessageBoxIcon.Information);
                }
            }
            else
                XtraMessageBox.Show(box.NotData,box.Caption,MessageBoxButtons.OK,MessageBoxIcon.Warning);
        }
        #endregion

        #region ham viet them

        private void LoadData()
        {
            grd_Data.DataSource = vp_bll.LayDuLieu();
        }

        #endregion
    }
}
